<?php

/**
 * @author christian
 * @copyright 2011
 *  
 */


$ruta=$_SERVER['DOCUMENT_ROOT']."/viventura"."/MDTour";
include_once("$ruta/star.php");
include_once("$ruta/MDTourGeneral/CDrawPageReg.php");

if (!isset($_POST['action'])) {$_POST['action'] = "NULL"; }
if (!isset($_POST['OrdTrack'])) {$_POST['OrdTrack'] = "NULL"; }
if (!isset($_POST['DateBegTrack'])) {$_POST['DateBegTrack'] = "NULL"; }
if (!isset($_POST['DateEndTrack'])) {$_POST['DateEndTrack'] = "NULL"; }
if (!isset($_POST['idTrack'])) {$_POST['idTrack'] = "NULL"; }
if (!isset($_POST['idPackTour'])) {$_POST['idPackTour'] = "NULL"; }

$action = $_POST['action'];
$OrdTrack = $_POST['OrdTrack'];
$DateBegTrack = $_POST['DateBegTrack'];
$DateEndTrack = $_POST['DateEndTrack'];
$idTrack = $_POST['idTrack'];
$idPackTour = $_POST['idPackTour'];

$DateEndTrack=CDrawPageReg::getInstance()->amysql($DateEndTrack);



/// Dos opciones, inicio del tour y en cualquier punto del tour y otros

// inicia el tour, consulta el dia de inicio del tour y se le asigna este inicio y como final lo que se ingreso

if($action==0){
    
    
        $DateBegTrack=CDrawPageReg::getInstance()->amysql($DateBegTrack);
        
        $firstTourTrack = "insert into tour_has_track (idTourTrack, OrdTrack, DateBegTrack, DateEndTrack, TOUR_idTour, TRACK_idTrack,RESTAURANT_idRest) VALUES 
                        (NULL,1,'$DateBegTrack','$DateEndTrack','$idPackTour','$idTrack','13000')";
        
        CModelo::getInstance()->insertData($firstTourTrack);
      
            
}
  
elseif($action==1){
    
    // Insercion simple uno después de otro
    

    // Insercion compleja con track después del insertado       
    
    $query = "select OrdTrack from tour_has_track where TOUR_idTour = '$idPackTour' order by OrdTrack DESC limit 0,1";
    $cuenta = CModelo::getInstance()->getData($query);
    
    foreach($cuenta as $c=>$r ){
        $table1 = $r ;
        foreach($table1 as $c1 => $r1 ){
            $total=$r1;
        }
    }
    
 
    if($OrdTrack<$total){
            
            $InsertOrdTrack = $OrdTrack+1;
            
            
            $thTourTrack = "insert into tour_has_track (idTourTrack, OrdTrack, DateBegTrack, DateEndTrack, TOUR_idTour, TRACK_idTrack,RESTAURANT_idRest) VALUES 
                        ('NULL','$InsertOrdTrack','$DateBegTrack','$DateEndTrack','$idPackTour','$idTrack','13000')";
            
           
            $idTourTrack = CModelo::getInstance()->insertData($thTourTrack);
            
            
          //  $query = "select DateBegTrack from tour_has_track where TOUR_idTour = '$idPackTour' and OrdTrack='$InsertOrdTrack'";
          //  $next = CModelo::getInstance()->getData($query);
    
           // foreach($next as $c=>$r ){
             //   $table1 = $r ;
             //   foreach($table1 as $c1 => $r1 ){
              //      $nextDateBegTrack=$r1;
            //    }
          ///  }
        
       // echo $nextDateBegTrack;
        $query = "SELECT DATEDIFF ('$DateEndTrack','$DateBegTrack')";
      
        $diff = CModelo::getInstance()->getData($query);
        
         foreach($diff as $c=>$r ){
                $table1 = $r ;
                foreach($table1 as $c1 => $r1 ){
                $desp=$r1;
                }
         }
            
            
            
            moveTracks($desp,$OrdTrack,$idPackTour,$total);
            
        
     }
     else{
          $OrdTrack = $OrdTrack+1;
    
          $thTourTrack = "insert into tour_has_track (idTourTrack, OrdTrack, DateBegTrack, DateEndTrack, TOUR_idTour, TRACK_idTrack,RESTAURANT_idRest) VALUES 
                        ('NULL','$OrdTrack','$DateBegTrack','$DateEndTrack','$idPackTour','$idTrack','13000')";
                             
          $idTourTrack = CModelo::getInstance()->insertData($thTourTrack);
          
          
         
     }
     
     
     
     
     
     }
     
     

    function moveTracks ($desp,$order,$idPackTour,$sizeTour){
        
            $order = $order+1;
                          
                $actualizar = "UPDATE tour_has_track SET DateBegTrack=DATE_ADD(DateBegTrack,INTERVAL '$desp' DAY), DateEndTrack=DATE_ADD(DateEndTrack,INTERVAL '$desp' DAY), OrdTrack=OrdTrack+1 WHERE (OrdTrack >'$order' AND TOUR_idTour ='$idPackTour')";
                //$actualizar = $string. ",";    
                
             CModelo::getInstance()->insertData($actualizar);   
    }

        // echo "<script> parent.location.href = '/viventura/MDTour/MDItinerary/MDEItinerary.php?codigo=$idPackTour' </script>";
        
    

?>